package net.betou.trade.entity.main;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.betou.trade.entity.main.base.BaseRole;

import org.apache.commons.lang.StringUtils;

public class Role extends BaseRole{

	private static final long serialVersionUID = 7555400701848999090L;
	
	/**
	 * 获取拥有者的名称
	 * 此处做异常处理，是因为数据间的约束并不存在
	 * 所以可能会有无效的数据
	 * @return
	 */
	public String getParentname( ){
		AgentAndAdmins admin = this.getAdmin( );
		
		if( admin != null ){
			return admin.getUsername( );
		}
		
		return null;
	}
	
	public Role( ) {
		super( );
	}
	
	public Role( Integer id ){
		super( id );
	}
	
	public Role( Integer id, String name, String description, Date createTime, Set< String > perms ) {
		super( id, name, description, createTime, perms );
	}
	
	public void initToDefault( String identity, String name, AgentAndAdmins admin, Integer level, Set< String > perms ){
		this.setAdmin( admin );
		this.setRoleDefault( false );
		this.setCreateTime( new Date( ) );
		this.setManager( false );
		this.setDescription( null );
		this.setAdmin( admin );
		this.setLevel( level );
		this.setAllowRead( true );
		this.setName( name );
		this.setIdentity( identity );
		this.setPerms( copyPerms( perms ) );
		this.setAllowInit( false );
	}
	
	
	/**
	 * 分组保存初始化
	 */
	public void init( ){
		this.setCreateTime( new Date( ) );
		if( StringUtils.isBlank( getDescription() )){
			this.setDescription( null ); // 设置为null, 后台好处理
		}
		
		if( getLevel ( ) == null ){
			setLevel( 10000 ); // 设置一个初始化等级，10000
		}
		
		this.setRoleDefault( false ); // 添加进来的都为非系统默认
	}
	
	public void saveToSystem( ){
		init( );
		this.setManager( true );
		this.setAllowRead( false ); // 系统的角色不允许被读取
		this.setIdentity( null ); // 没有标识符
		this.setAdmin( null ); // 系统角色没有相关的管理员信息
	}
	
	public void update( String description, Integer level, String name ){
		this.setDescription( StringUtils.isBlank( description ) ? null : description );
		this.setName( name );
		this.setLevel( level );
	}
	
	/**
	 * @version 1.1
	 * @param allowInit
	 * @param description
	 * @param level
	 * @param name
	 */
	public void update( Boolean allowInit, String description, Integer level, String name ){
		this.setAllowInit( allowInit == null ? false : allowInit );
		update( description, level, name );
	}
	
	public void saveToAgency( AgentAndAdmins admin ){
		init( );
		this.setManager( false );
		this.setAllowRead( true );
		this.setAdmin( admin );
		this.setIdentity( null );
	}
	
	public void addToRoleExts( RoleExt role ) {
		if( role == null ){
			return;
		}

		Set< RoleExt > set = this.getRoleExt( );

		if( set == null ){
			set = new HashSet< RoleExt >( );
			setRoleExt( set );
		}
		set.add( role );

	}
	
	/**
	 * 获取RoleExt, 存放在Map中
	 * @return
	 */
	public Map< String, RoleExt > getSearchSettingRoleExt( ){
		Set< RoleExt > set = getRoleExt( );
		if( set == null || set.size( ) == 0 ){
			return null;
		}
		Map< String, RoleExt > extMap = new HashMap< String, RoleExt >( );
		
		Iterator< RoleExt > it = set.iterator( );
		
		while( it.hasNext( ) ){
			RoleExt ext = it.next( );
			String key = ext.getKey( );
			extMap.put( key, ext );
		}
		
		return extMap;
		
	}
	
	/**
	 * 复制权限
	 * @param perms	绝对不会为空
	 * @return
	 */
	private Set< String > copyPerms( Set< String > perms ){
		Set< String > sets = new HashSet< String >( perms.size( ) );
		
		Iterator< String > it = perms.iterator( );
		
		while( it.hasNext( ) ){
			sets.add( it.next( ) );
		}
		
		return sets;
		
	}
}
